package com.yonyou.marketing.app.api.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.AppResponse;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.dto.information.ActivityInfoDto;
import org.marketing.common.dto.information.ActivityItemsDto;
import org.marketing.common.dto.information.ActivityRecordDto;
import org.marketing.common.dto.information.ReadingDto;
import org.marketing.common.dto.leads.ThreadcarDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.common.annotation.ParamValid;
import com.yonyou.marketing.app.api.constant.AppConstant;
import com.yonyou.marketing.app.api.model.TmCarKnowledges;
import com.yonyou.marketing.app.api.service.AppInformationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author Administrator
 * @date 2017年6月30日
 */
@RestController
@RequestMapping(value = AppConstant.BASE_PATH + "/" + AppConstant.VERSION)
@Api("APP资讯活动API")
public class AppInformationController {

	private Logger          logger = Logger.getLogger(AppInformationController.class);

	@Autowired
    private AppInformationService appInformationService;
	
	/**
	 * 用车知识查询
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/findCarKnow", method = RequestMethod.POST)
	@ApiOperation(value = "用车知识查询", notes = "用车知识查询")
	@ParamValid
	public AppResponse<PageInfo<TmCarKnowledges>> findCarKnow(@RequestParam(required=false,defaultValue="0") Integer pageNum
    		,@RequestParam(required=false,defaultValue="10") Integer pageSize
    		,@RequestParam(required=false,defaultValue="") String title) {
		logger.info("AppInformationController.findCarKnow==>start...");
		try {
			PageInfo<TmCarKnowledges> result = appInformationService.findCarKnow(pageNum,pageSize,title);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.findCarKnow==>end...");
		}
	}
	/**
	 * 用车知识查询
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/findCarKnowDetail", method = RequestMethod.POST)
	@ApiOperation(value = "用车知识详情", notes = "用车知识详情")
	@ParamValid
	public AppResponse<List<TmCarKnowledges>> findCarKnowDetail(@RequestParam(required=true,defaultValue="0") Integer ckId) {
		logger.info("AppInformationController.findCarKnowDetail==>start...");
		try {
			List<TmCarKnowledges> result = appInformationService.findCarKnowDetail(ckId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.findCarKnowDetail==>end...");
		}
	}
	/**
	 * 获取新闻资讯内容
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/sharepage/newsContent", method = RequestMethod.POST)
	@ApiOperation(value = "获取新闻资讯内容", notes = "获取新闻资讯内容")
	@ParamValid
	public AppResponse<Map<String, Object>> getNewsActivityContent(@RequestParam(required=true,defaultValue="0") Integer naiId
			,@RequestParam(required=true,defaultValue="0") Integer type) {
		logger.info("AppInformationController.getNewsActivityContent==>start...");
		try {
			Map<String, Object> result = appInformationService.getNewsActivityContent(naiId,type);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.getNewsActivityContent==>end...");
		}
	}
	/**
	 * 保存留资信息
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/sharepage/saveCapitalInfor", method = RequestMethod.POST)
	@ApiOperation(value = "保存留资信息", notes = "保存留资信息")
	@ParamValid
	public AppResponse<Map<String, Object>> saveCapitalInfor(@RequestParam(required=true,defaultValue="0") Long naiId
			,@RequestParam(required=true,defaultValue="0") Long userId
			,@RequestParam(required=true,defaultValue="") String friendphone
			,@RequestParam(required=true,defaultValue="") String friendName) {
		logger.info("AppInformationController.saveCapitalInfor==>start...");
		try {
			ThreadcarDto threadcarDto = new ThreadcarDto();
			threadcarDto.setUserId(userId);
			threadcarDto.setNaiId(naiId);
			threadcarDto.setFriendphone(friendphone);
			threadcarDto.setFriendname(friendName);
			Map<String, Object> result = appInformationService.saveCapitalInfor(threadcarDto);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.saveCapitalInfor==>end...");
		}
	}
	/**
	 * 活动填写项目查询
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/sharepage/activityItems", method = RequestMethod.POST)
	@ApiOperation(value = "活动填写项目查询", notes = "活动填写项目查询")
	@ParamValid
	public AppResponse<List<ActivityItemsDto>> getActivityItems(@RequestParam(required=true,defaultValue="0") Long naiId) {
		logger.info("AppInformationController.getActivityItems==>start...");
		try {
			List<ActivityItemsDto> result = appInformationService.getActivityItems(naiId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.getActivityItems==>end...");
		}
	}
	/**
	 * 新增报名人信息
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/sharepage/insertPotentialUser", method = RequestMethod.POST)
	@ApiOperation(value = "新增报名人信息", notes = "新增报名人信息")
	@ParamValid
	public AppResponse<Map<String, Object>> insertPotentialUser(@RequestParam(required=false,defaultValue="0") Integer naiId
			,@RequestParam(required=false,defaultValue="") String applyName
			,@RequestParam(required=false,defaultValue="") String applyTel
			,@RequestParam(required=false,defaultValue="") Short age
			,@RequestParam(required=false,defaultValue="") String weixinId
			,@RequestParam(required=false,defaultValue="") Long qqNum
			,@RequestParam(required=false,defaultValue="") String weiboId
			,@RequestParam(required=false,defaultValue="") Short sex
			,@RequestParam(required=false,defaultValue="") Short vehiclesNum
			,@RequestParam(required=false,defaultValue="") String participateDealer
			,@RequestParam(required=false,defaultValue="") Short type
			,@RequestParam(required=false,defaultValue="") String brandName
			,@RequestParam(required=false,defaultValue="") String modelName
			,@RequestParam(required=false,defaultValue="") String displacement
			,@RequestParam(required=false,defaultValue="") String color
			,@RequestParam(required=false,defaultValue="") String registNo
			,@RequestParam(required=false,defaultValue="") Date salesDate
			,@RequestParam(required=false,defaultValue="") String buyeDealer
			,@RequestParam(required=false,defaultValue="") Short isUseed
			,@RequestParam(required=false,defaultValue="") String expandField3
			,@RequestParam(required=false,defaultValue="") String expandField2
			,@RequestParam(required=false,defaultValue="") String expandField1
			,@RequestParam(required=false,defaultValue="") Date applyDate
			,@RequestParam(required=false,defaultValue="") Integer createBy
			,@RequestParam(required=false,defaultValue="") Date updateDate
			,@RequestParam(required=false,defaultValue="") Integer updateBy) {
		logger.info("AppInformationController.saveCapitalInfor==>start...");
		try {
			ActivityRecordDto activityRecordDto = new ActivityRecordDto();
			activityRecordDto.setNaiId(naiId);
			activityRecordDto.setApplyName(applyName);
			activityRecordDto.setApplyTel(applyTel);
			activityRecordDto.setAge(age);
			activityRecordDto.setWeixinId(weixinId);
			activityRecordDto.setQqNum(qqNum);
			activityRecordDto.setWeiboId(weiboId);
			activityRecordDto.setSex(sex);
			activityRecordDto.setVehiclesNum(vehiclesNum);
			activityRecordDto.setParticipateDealer(participateDealer);
			activityRecordDto.setType(type);
			activityRecordDto.setBrandName(brandName);
			activityRecordDto.setModelName(modelName);
			activityRecordDto.setDisplacement(displacement);
			activityRecordDto.setColor(color);
			activityRecordDto.setRegistNo(registNo);
			activityRecordDto.setSalesDate(salesDate);
			activityRecordDto.setBuyeDealer(buyeDealer);
			activityRecordDto.setIsUseed(isUseed);
			activityRecordDto.setExpandField3(expandField3);
			activityRecordDto.setExpandField2(expandField2);
			activityRecordDto.setExpandField1(expandField1);
			activityRecordDto.setApplyDate(applyDate);
			activityRecordDto.setCreateBy(createBy);
			activityRecordDto.setUpdateDate(updateDate);
			activityRecordDto.setUpdateBy(updateBy);
			Map<String, Object> result = appInformationService.insertPotentialUser(activityRecordDto);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.saveCapitalInfor==>end...");
		}
	}
	/**
	 * 统计阅读量
	 * 
	 * @author Administrator TODO description
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/sharepage/readingNum", method = RequestMethod.POST)
	@ApiOperation(value = "统计阅读量", notes = "统计阅读量")
	@ParamValid
	public AppResponse<Map<String, Object>> saveReadingNum(
			 @RequestParam(required=false,defaultValue="") Date createDate
			,@RequestParam(required=false,defaultValue="") Long objectId
			,@RequestParam(required=false,defaultValue="") String objectType
			,@RequestParam(required=false,defaultValue="") String phone
			,@RequestParam(required=false,defaultValue="") Integer readingNum
			,@RequestParam(required=false,defaultValue="") Long readingStatisticsId
			,@RequestParam(required=false,defaultValue="") Date updateDate
			,@RequestParam(required=false,defaultValue="") String userId) {
		logger.info("AppInformationController.saveReadingNum==>start...");
		try {
			ReadingDto readingDto2 = new ReadingDto();
	    	readingDto2.setCreateDate(createDate);
	    	readingDto2.setObjectId(objectId);
	    	readingDto2.setObjectType(objectType);
	    	readingDto2.setPhone(phone);
	    	readingDto2.setReadingNum(readingNum);
	    	readingDto2.setReadingStatisticsId(readingStatisticsId);
	    	readingDto2.setUpdateDate(updateDate);
	    	readingDto2.setUserId(userId);
			Map<String, Object> result = appInformationService.saveReadingNum(readingDto2);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.saveReadingNum==>end...");
		}
	}
	/**
	 * 分享成功自动发放卡券
	 * 
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/sharepage/sendTicket", method = RequestMethod.POST)
	@ApiOperation(value = "分享成功自动发放卡券", notes = "分享成功自动发放卡券")
	@ParamValid
	public AppResponse<Map<String, Object>> sendTicket(
			 @RequestParam(required=false,defaultValue="") Long objectId
			,@RequestParam(required=false,defaultValue="") String userId) {
		logger.info("AppInformationController.sendTicket==>start...");
		try {
			ReadingDto readingDto2 = new ReadingDto();
	    	readingDto2.setObjectId(objectId);
	    	readingDto2.setUserId(userId);
			Map<String, Object> result = appInformationService.saveReadingNum(readingDto2);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.sendTicket==>end...");
		}
	}
	/**
	 * 已报名信息列表查询
	 * 
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/activityLists", method = RequestMethod.POST)
	@ApiOperation(value = "已报名信息列表查询", notes = "已报名信息列表查询")
	@ParamValid
	public AppResponse<List<ActivityRecordDto>> getActivityList(
			@RequestParam("userId") String userId) {
		logger.info("AppInformationController.getActivityList==>start...");
		try {
			List<ActivityRecordDto> result = appInformationService.getActivityList(userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.getActivityList==>end...");
		}
	}
	/**
	 * 已报名详情查询
	 * 
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/activityInfo", method = RequestMethod.POST)
	@ApiOperation(value = "已报名详情查询", notes = "已报名详情查询")
	@ParamValid
	public AppResponse<List<ActivityRecordDto>> getActivityInfo(
			@RequestParam("applyId") Long applyId) {
		logger.info("AppInformationController.getActivityInfo==>start...");
		try {
			List<ActivityRecordDto> result = appInformationService.getActivityInfo(applyId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.getActivityInfo==>end...");
		}
	}
	/**
	 * 我能看到的资讯查询 
	 * 
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/myNews", method = RequestMethod.POST)
	@ApiOperation(value = "我能看到的资讯查询 ", notes = "我能看到的资讯查询 ")
	@ParamValid
	public AppResponse<List<ActivityInfoDto>> getMyNews(
			@RequestParam("userId") String userId,@RequestParam("activityType") Long activityType) {
		logger.info("AppInformationController.getMyNews==>start...");
		try {
			List<ActivityInfoDto> result = appInformationService.getMyNews(userId,activityType);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.getMyNews==>end...");
		}
	}
	/**
	 * 我能看到的活动查询
	 * 
	 * @date 2017年4月17日
	 * @return
	 */
	@RequestMapping(value = "/myActivities", method = RequestMethod.POST)
	@ApiOperation(value = "我能看到的活动查询", notes = "我能看到的活动查询")
	@ParamValid
	public AppResponse<List<ActivityInfoDto>> getMyActivities(
			@RequestParam("userId") String userId) {
		logger.info("AppInformationController.getMyNews==>start...");
		try {
			List<ActivityInfoDto> result = appInformationService.getMyActivities(userId);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new AppResponse<>(ResultMsgConstants.APP_RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("AppInformationController.getMyNews==>end...");
		}
	}
}
